package com.async.config;


import com.async.annotation.AsyncExec;
import com.async.constant.AsyncConstant;
import com.async.handler.context.AsyncStrategyContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
@ConditionalOnProperty(prefix = "async", value = "enabled", havingValue = "true")
public class AsyncAspect {

    @Autowired
    private ApplicationEventPublisher publisher;


    @Around("@annotation(asyncExec)")
    public Object around(ProceedingJoinPoint joinPoint, AsyncExec asyncExec) throws Throwable {
        // 给每个线程添加一个标记，防止死循环
        if (AsyncConstant.PUBLISH_EVENT.get()){
            try {
                return joinPoint.proceed();
            }finally {
                AsyncConstant.PUBLISH_EVENT.remove();
            }
        }else {
            AsyncStrategyContext context = new AsyncStrategyContext();
            context.setJoinPoint(joinPoint);
            context.setAsyncExec(asyncExec);

            publisher.publishEvent(context);
            log.info("异步执行事件发布成功，策略类型：{}，业务描述：{}", asyncExec.type(), asyncExec.remark());

            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Class<?> returnType = signature.getMethod().getReturnType();
            if (returnType != Void.TYPE && returnType.isPrimitive()) {
                // 8种基本类型需特殊处理（byte、short、char、int、long、float、double、boolean）
                return returnType == Boolean.TYPE ? Boolean.TRUE : 1;
            }
            return null;
        }
    }
}
